<?php 
	namespace app\admin\controller;
	use think\Controller;
	use think\Db;
	use think\Session;

	//后台登录注销
	class Login extends Controller
	{

		//登录界面
		public function getlogin()
		{
			return $this->fetch('login/login');
		}

		//执行登录
		public function postdologin()
		{
			//字段为空便不执行登录
			if($_POST['phone']==""||$_POST['password']==""){
				//返回登录界面
				$this->error("请填写完整","/login/login");
				exit;

			}
			$request=request();
			$data=$request->post();
			$result=Db::table('user')->where('phone',$data['phone'])->select();

			//对比数据库
			if($result){
				if($result[0]['password']==$data['password']){
                     //如果账户禁用便不让其登录
                     if($result[0]['static']==2)
                     {
                     	$this->error('账号已禁用','/login/login');
                     }
					//把登录信息存储在session中
					Session::set('aname',$result[0]['username']);
					//将登录时间存在SESSION
					Session::set('times',time());
					//将用户ID存储在SESSION

					Session::set('ids',$result[0]['id']);

					$list=Db::query("select n.name,n.c,n.a from lists as l,list as n where l.nid=n.id and l.details={$result[0]['details']}");
	
					//存储权限信息到session中
					$nodelist['admin'][]='getindex';
 
					foreach($list as $v){
                        //给数组$nodelist 赋值
                        $nodelist[$v['c']][]=$v['a'];

                        //如果具有add 添加insert
                        if($v['a']=="getadd"){
                            $nodelist[$v['c']][]="postinsert";
                        }

                        //如果具有edit 添加update
                        if($v['a']=="getedit"){
                            $nodelist[$v['c']][]="postupdate";
                        }

                        if($v['a']=='getadds'){
                            $nodelist[$v['c']][]="postinserts";
                   	    }
                   	     if($v['a']=='getessay'){
                            $nodelist[$v['c']][]="postessayadd";
                   	    }

          
                    }
                    Session::set("nodelist",$nodelist);
 
					$this->success('欢迎登录','/admin/index');
				}else{
					$this->error('密码错误','/login/login');
				}
			}else{
				$this->error('不存在的号码','/login/login');
			}
		}

		//注销操作
		public function getlogout()
		{

			$list=Db::table('user')->where('id',session::get('ids'))->find();
			$list['login']=session::get('times');
			$list=Db::table('user')->where('id',session::get('ids'))->update($list);
			session::delete('ids');
			session::delete('times');
			Session::delete('aname');
			$this->success('安全退出','/login/login');
		}
	}
 ?>